module.exports = function(e, t, r) {
    var n = require("../util"), a = require("../common/res"), o = require("../common/perf"), i = null, s = r.documentElement, c = t.innerWidth || s.clientWidth || r.body.clientWidth, h = t.innerHeight || s.clientHeight || r.body.clientHeight, d = t.navigator.connection, l = {
        sr: screen.width + "x" + screen.height,
        vp: c + "x" + h,
        ct: d ? d.effectiveType || d.type : ""
    }, u = {}, g = function(e, t, a, o, i) {
        if (t === undefined) {
            var s, c;
            if (!u[e]) {
                s = new RegExp(e + "=([^;]+)");
                try {
                    c = s.exec(r.cookie);
                } catch (d) {
                    return n.warn("[retcode] can not get cookie:", d), null;
                }
                c && (u[e] = c[1]);
            }
            return u[e];
        }
        var h = e + "=" + t;
        o && (h += "; domain=" + o), h += "; path=" + (i || "/"), a && (h += "; max-age=" + a);
        try {
            return r.cookie = h, !!r.cookie;
        } catch (d) {
            return n.warn("[retcode] can not set cookie: ", d), !1;
        }
    }, f = function(e) {
        var t = e._conf.uid || g("_nk_") || g("_bl_uid");
        if (!t) {
            t = n.uu();
            if (!g("_bl_uid", t, 15552e3)) return null;
        }
        return t;
    };
    return n.ext(e.prototype, {
        activeErrHandler: function(e) {
            return i && !e ? this : (i = this, this);
        },
        errorHandler: function(e) {
            if (!e) return this;
            var t = e.type;
            if ("error" === t) {
                var r = e.target || e.srcElement;
                !(!r || !r.tagName || e.message || e.filename || e.lineno || e.colno) ? this.resourceErrorHandler(e) : this.error(e.error || {
                    message: e.message
                }, e);
            } else "unhandledrejection" === t && n.T(e.reason, "Error") && n.checkAutoError(e.reason) && this.error(e.reason);
            try {
                this.getConfig("behavior") && this.reportBehavior && this.reportBehavior();
            } catch (e) {}
            return this;
        },
        resourceErrorHandler: function(e) {
            var t = this, r = e.target || e.srcElement;
            try {
                var a = t.getSrc(r), o = "string" == typeof r.tagName ? r.tagName.toLowerCase() : "", i = t.getXPath(r, 5);
                return t._lg("resourceError", {
                    src: a && a.substring(0, 1e3),
                    node_name: o,
                    xpath: i
                }), t;
            } catch (s) {
                return n.warn("[ARMS] resourceErrorHandler error :", s), t;
            }
        },
        getSrc: function(e) {
            var t = e.src || e.href;
            try {
                if (!t) {
                    var r = "object" === ("string" == typeof e.tagName ? e.tagName.toLowerCase() : ""), n = e.getAttribute("classid") && "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" === e.getAttribute("classid").toLowerCase() || "application/x-shockwave-flash" === e.getAttribute("type");
                    r && n && (t = e.getAttribute("data") || e.getAttribute("codebase")), t || (t = e.outerHTML || e.innerHTML);
                }
            } catch (a) {
                t = "";
            }
            return t;
        },
        getXPath: function(e, t) {
            var r = e.id ? "#" + e.id : "", n = "string" == typeof e.className ? "." + e.className.split(" ").join(".") : "", a = ("string" == typeof e.tagName ? e.tagName.toLowerCase() : "") + r + n;
            return e.parentNode && e.parentNode.tagName && t - 1 != 0 ? this.getXPath(e.parentNode, t - 1) + " > " + a : a;
        },
        sendPerformance: function(e) {
            var t = this;
            t.onReady(function() {
                var r = o();
                r && r.load && r.load > 0 && (r.page = t.getPage(!0), e && (r = n.ext(r, e)), r.autoSend = !0, 
                t.performance(r));
            });
        },
        sendResources: function(e) {
            var t = this;
            t.onReady(function() {
                var r = a();
                r && r.load && r.load > 0 && (r.load && r.load <= 2e3 || r.load && r.load <= 8e3 && Math.random() > .05 || (r.page = t.getPage(!0), 
                r.dl = location.href, e && (r = n.ext(r, e)), t._lg("res", r, t.getConfig("sample"))));
            });
        },
        sendPV: function() {
            var e = this;
            e.onReady(function() {
                var n = function(e) {
                    var n = f(e), a = t.devicePixelRatio || 1;
                    return {
                        uid: n,
                        dt: r.title,
                        dl: location.href,
                        dr: r.referrer,
                        dpr: a.toFixed(2),
                        de: (r.characterSet || r.defaultCharset || "").toLowerCase(),
                        ul: s.lang,
                        begin: Date.now()
                    };
                }(e);
                n && n.uid && e._lg("pv", n, e.getConfig("pvSample"));
            });
        },
        commonInfo: function() {
            return l.uid = f(this), l.sid = function(e) {
                if (e.session) return e.session;
                var t;
                try {
                    if ("object" == typeof window && "object" == typeof sessionStorage && "function" == typeof sessionStorage.getItem) return "string" == typeof (t = sessionStorage.getItem("_bl_sid")) ? (e.session = t, 
                    t) : (t = n.uu(), e.session = t, "function" == typeof sessionStorage.setItem && sessionStorage.setItem("_bl_sid", t), 
                    t);
                } catch (r) {
                    n.warn("[ARMS] getSid error :", r);
                }
                return e.session = t = n.uu(), t;
            }(this), l;
        },
        handleUnload: function(e) {
            var t = Date.now();
            if (t - this._lastUnload < 200) return this;
            this._lastUnload = t, this.sendHealth(e), this.speedCache && (this._lg("speed", this.speedCache), 
            this.speedCache = null, clearTimeout(this.speedTimmer)), this.clear(!0);
        },
        bindHashChange: function(e) {
            var r = this;
            if (!e ^ r.hashChangeHandler) return r;
            e ? (r.hackHistoryState(), r.hashChangeHandler = function(e) {
                var t = r._conf.parseHash(location.hash);
                t && r.setPage(t, !1 !== e);
            }, r.stateChangeHandler = function(e) {
                var t = r._conf.parseHash(e.detail);
                t && r.setPage(t);
            }, n.on(t, "hashchange", r.hashChangeHandler), n.on(t, "historystatechange", r.stateChangeHandler), 
            r.hashChangeHandler(!1)) : (n.off(t, "hashchange", r.hashChangeHandler), n.off(t, "historystatechange", r.stateChangeHandler), 
            r.hashChangeHandler = null, r.stateChangeHandler = null);
        },
        initHandler: function() {
            var e = this;
            if (e.hasInitHandler) return e;
            var r = e._conf;
            return n.on(t, "beforeunload", function() {
                e.handleUnload(0);
            }), e.bindHashChange(r.enableSPA), e.activeErrHandler(!1), e.hasInitHandler = !0, 
            e;
        }
    }), n.on(t, "error", function(e) {
        i && i.errorHandler(e);
    }, !1, !0).on(t, "unhandledrejection", function(e) {
        i && i.errorHandler(e);
    }), e;
};